/*
 * @Description:
 * @Author: Zzt
 * @Date: 2023-02-20 11:23:06
 * @LastEditTime: 2023-02-28 17:28:30
 */
import {
  GET_CHANNEL_LIST,
  CELECT_CHANNEL,
  DEL_CHANNELLIST,
  DEL_ANNOUNCELIST,
  ADD_ANNOUNCELIST,
  EDIT_ANNOUNCE_LIST,
  GET_ANNOUNCE_LIST,
  SET_CHANNEL,
  SET_SELECT_KEY
} from '../constants';
import { delItemByIdArray, updateItemById, insertItem, insertItemAfter } from '@/utils/list';

export const AnnouncementMgtReducer = (state, action) => {
  switch (action.type) {
    case GET_CHANNEL_LIST:
      return {
        ...state,
        channelList: action.data
      };
    case CELECT_CHANNEL:
      return { ...state, noticeTypeId: action.data };
    /* 新增频道 */
    case SET_CHANNEL:
      return { ...state, channelList: insertItemAfter(action.data, state.channelList) };
    /* 删除频道 */
    case DEL_CHANNELLIST:
      return { ...state, channelList: delItemByIdArray(action.ids, state.channelList) };
    /* 获取公告列表 */
    case GET_ANNOUNCE_LIST:
      return { ...state, announceList: action.data };
    case ADD_ANNOUNCELIST: {
      return { ...state, announceList: insertItem(action.data, state.announceList) };
    }
    case SET_SELECT_KEY: {
      return { ...state, selectItems: action.data };
    }
    /* 删除 */
    case DEL_ANNOUNCELIST: {
      return { ...state, announceList: delItemByIdArray(action.ids, state.announceList) };
    }
    /* 修改 */
    case EDIT_ANNOUNCE_LIST: {
      return {
        ...state,
        announceList: updateItemById(action.data.id, action.data, state.announceList)
      };
    }
    default:
      console.log('untouched', action);
      return state;
  }
};
